package user_c

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"zg320250108/dao/mysql"
	"zg320250108/requests"
)

func Login(c *gin.Context) {

	var json requests.LoginRequest

	if err := c.ShouldBindJSON(&json); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, gin.H{"status": "you are logged in"})
}

func Register(c *gin.Context) {

	// 接收参数
	var data requests.RegisterRequest

	if err := c.ShouldBindJSON(&data); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	// 用户名有没有是否已存在
	user, err := mysql.GetUserByUsername(data.Username)
	if err != nil {

		c.JSON(http.StatusOK, gin.H{
			"code": 100,
			"msg":  "失败",
			"data": gin.H{}})

		return
	}

	//创建用户
	user, err = mysql.CreateUserByUsernameAndPassword(data.Username, data.Password)
	if err != nil {
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 100,
		"msg":  "失败",
		"data": user})

}

func GetUserDetail(c *gin.Context) {

	c.JSON(http.StatusOK, gin.H{"status": "you are logged in"})
}

func Upload(c *gin.Context) {

	c.JSON(http.StatusOK, gin.H{"status": "you are logged in"})
}

func UpdateUserDetail(c *gin.Context) {

	c.JSON(http.StatusOK, gin.H{"status": "you are logged in"})
}
